package jyan.sango.crazy.sangoedit;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.filechooser.FileNameExtensionFilter;

import jyan.sango.crazy.core.debug.DEBUG;
import jyan.sango.crazy.core.debug.SangoException;
import jyan.sango.crazy.core.xml.ReadCard;
import jyan.sango.crazy.core.xml.XMLType;

import com.jgoodies.forms.factories.DefaultComponentFactory;

public class OpenWindow {

	private JFrame frmSangoedit;
	private JTextField textField;
	private final Action action = new SwingAction();
	private List<?> list = null;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					OpenWindow window = new OpenWindow();
					window.frmSangoedit.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	private OpenWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	@SuppressWarnings({"unchecked", "rawtypes"})
	private void initialize() {
		frmSangoedit = new JFrame();
		frmSangoedit.setResizable(false);
		frmSangoedit.setTitle("SangoEdit");
		frmSangoedit.setBounds(100, 100, 450, 300);
		frmSangoedit.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel panel = new JPanel();
		GroupLayout groupLayout = new GroupLayout(frmSangoedit.getContentPane());
		groupLayout.setHorizontalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addComponent(panel,
				GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE));
		groupLayout.setVerticalGroup(groupLayout.createParallelGroup(
				Alignment.LEADING).addComponent(panel,
				GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE));

		JLabel label = new JLabel(Defination.PATHSTRING);

		textField = new JTextField();
		textField.setEditable(false);
		textField.setColumns(10);
		JButton openbutton = new JButton(Defination.OPEN);
		openbutton.setAction(action);

		JLabel label_1 = DefaultComponentFactory.getInstance().createLabel(
				Defination.OPEN);

		JComboBox typecomboBox = new JComboBox();
		typecomboBox.setModel(new DefaultComboBoxModel(XMLType.values()));
		GroupLayout gl_panel = new GroupLayout(panel);
		gl_panel.setHorizontalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addComponent(label)
												.addComponent(label_1))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.TRAILING)
												.addGroup(
														gl_panel.createSequentialGroup()
																.addComponent(
																		typecomboBox,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addPreferredGap(
																		ComponentPlacement.RELATED,
																		279,
																		Short.MAX_VALUE)
																.addComponent(
																		openbutton))
												.addComponent(
														textField,
														Alignment.LEADING,
														GroupLayout.DEFAULT_SIZE,
														297, Short.MAX_VALUE))
								.addContainerGap()));
		gl_panel.setVerticalGroup(gl_panel
				.createParallelGroup(Alignment.LEADING)
				.addGroup(
						gl_panel.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.BASELINE)
												.addComponent(label)
												.addComponent(
														textField,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.DEFAULT_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(ComponentPlacement.RELATED)
								.addGroup(
										gl_panel.createParallelGroup(
												Alignment.LEADING)
												.addComponent(openbutton)
												.addGroup(
														gl_panel.createParallelGroup(
																Alignment.BASELINE)
																.addComponent(
																		label_1)
																.addComponent(
																		typecomboBox,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.DEFAULT_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addContainerGap(204, Short.MAX_VALUE)));
		panel.setLayout(gl_panel);
		frmSangoedit.getContentPane().setLayout(groupLayout);
	}

	private class SwingAction extends AbstractAction {
		/**
		 * 
		 */
		private static final long serialVersionUID = 4221544964077893812L;
		private JFrame jframe = null;
		private FileNameExtensionFilter filter = null;
		private FilenameFilter filefilter = null;
		private JFileChooser fc = null;
		private Boolean systemtype = Boolean.TRUE;


		public SwingAction() {
			this.jframe = frmSangoedit;
			putValue(NAME, Defination.OPEN);
			putValue(SHORT_DESCRIPTION, "Some short description");
			systemtype = getSystemType();
			filefilter = new FilenameFilter() {
				@Override
				public boolean accept(File dir, String name) {
					// TODO Auto-generated method stub
					Matcher matcher = getMatcher("xml", name);
					if (matcher.find()) {
						System.out.println("find");
						return true;
					}
					return false;
				}
			};
			filter = new FileNameExtensionFilter("All XML files", "xml");
			fc = new JFileChooser("./");
			fc.setFileFilter(filter);
		}

		public SwingAction(final JFrame frmSangoedit) {
			if (jframe == null) {
				jframe = frmSangoedit;
			}

		}
		private Matcher getMatcher(String str, String machername) {
			Pattern pattern = Pattern.compile(str);
			Matcher matcher = pattern.matcher(machername);
			return matcher;

		}
		private Boolean getSystemType() {
			if (true)
				return Boolean.TRUE;
			@SuppressWarnings("unused")
			String sysname = System.getProperty("os.name");
			System.out.println(sysname);
			Matcher matcher = getMatcher("(win)|(Win)", sysname);

			if (matcher.find()) {
				DEBUG.Print(matcher.group());
				return Boolean.FALSE;
			}
			return Boolean.TRUE;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			Integer rval = null;
			if (systemtype) {
				fc.setDialogTitle(Defination.OPENFILE);
				rval = fc.showOpenDialog(jframe);
			} else {
				/*
				 * FileDialog fdi = new FileDialog(jframe, Defination.OPENFILE,
				 * FileDialog.LOAD); fdi.setFilenameFilter(filefilter);
				 * fdi.setVisible(true);
				 */

			}
			String pathfile = null;
			DEBUG.Print(rval);
			if (JFileChooser.APPROVE_OPTION == rval) {
				try {
					pathfile = fc.getSelectedFile().getCanonicalPath();
					DEBUG.Print(pathfile);
					list = ReadCard.getCardsWithFileName(pathfile);
				} catch (IOException e1) {
					new SangoException(e1.toString());
				} catch (SangoException e1) {
					DEBUG.Print(e1.toString());
				}
			}
		}
	}
}
